package map;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
public class MapDemo {
    public static void main(String[] args){
        //建立哈希表 并添加值
        Map map = new HashMap();
        map.put("Lucy",5);
        map.put("Lily",4);
        map.put("Han Meimei",5);
        map.put("Jim",3);
        map.put("Zou Encen",3);
        //查找lucy对应的值
        String namekey = "Lucy";
        Integer levelvalue = null;
        
        if (map.containsKey(namekey)){
            levelvalue = (Integer) map.get(namekey);
            System.out.println("Use namekey:" + namekey + " find levelvalue:" + levelvalue);
            System.out.println("-----------------------");
        }
        else
        {
            System.out.println("Does not contain namekey:" + namekey);
            System.out.println("-----------------------");
        }

        namekey = "zhansan";
        if (map.containsKey(namekey))
        {
            levelvalue = (Integer) map.get(namekey);
            System.out.println("Use namekey:" + namekey + " find levelvalue:" + levelvalue);
            System.out.println("-----------------------");
        }
        else
        {
            System.out.println("Does not contain namekey:" + namekey);
            levelvalue = (Integer) map.get(namekey);
            System.out.println("Use namekey:" + namekey + "/ find levelvalue:" + levelvalue + "/  If map does not contain namekey, it will return null, be carefull.");
            System.out.println("-----------------------");
        }
        //entrySet()方法返回此映射所包含的映射关系的 Set 视图。
        Iterator it = map.entrySet().iterator();
        Map.Entry entry = (Map.Entry) it.next();
        namekey=(String) entry.getKey();
        levelvalue=(Integer) entry.getValue();
        System.out.println("Use iterator to get the first entry is nameKey :"+namekey+"  levelValue :"+levelvalue);
        System.out.println("-----------------------");

        entry = (Map.Entry) it.next();
        namekey=(String) entry.getKey();
        levelvalue=(Integer) entry.getValue();
        System.out.println("Use iterator to get the second entry is nameKey :"+namekey+"  levelValue :"+levelvalue);
        System.out.println("-----------------------");
    }
}
